<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function product_sku_list_page()
{
    @$output = drupal_render(drupal_get_form('product_sku_list_form'));
    return $output;
}

function product_sku_list_form($form, &$form_state)
{
//    print_r($_GET['q']);
//    return;

//    $search_img = _is_mobile()?"":"<img src='".base_path()."sites/all/themes/tweme/images/search.png'>";
//    $res_img = _is_mobile()?"":"<img src='".base_path()."sites/all/themes/tweme/images/result.png'>";
//    $form['border_wrapper']['#markup'] = $search_img .'<div class="border_wrapper-search">';
//    $form['help']['#markup'] = _is_mobile()?"":'<div id="help"><a href="'.base_path().'" data-toggle="modal" target = "_blank">
//                                        <img src="'.base_path().'sites/all/themes/tweme/images/help.png"/>
//                                 </a></div>';

    $form['brand'] = array(
        '#attributes' => array('placeholder' => '品牌名字'),
        '#size' => 32,
        '#maxlength' => 32,
        '#type' => 'textfield',
        '#default_value' => !empty($form_state['values']['brand']) ? $form_state['values']['brand'] : '',
    );
//    "</div><div class='table-div'>$res_img<div class='table-border-div'>".."</div></div>"
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function product_sku_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
    global $user;
    // if($form_state['triggering_element']['#value'] == '查询'){
    $output = '';
    //表头
    $header = array(
        array('data' => '选择', 'class' => ['no']),
        array('data' => '品牌', 'class' => ['province']),
        array('data' => '肥料类别', 'class' => 'name'),
        array('data' => '配比', 'class' => 'name'),
        array('data' => 'SM', 'class' => 'name'),
        array('data' => '操作', 'class' => ['more']),
    );
    $query = db_select('product_sku', 'ps')->extend('PagerDefault')->extend('TableSort');
    $query->join('brand','b','b.brand_id = ps.brand_id');
    $query->fields('ps');
    $query->addField('b','brand_name');
    //模糊条件筛选
    if (!empty($form_state['values']['brand'])) {//模糊地址查询条件
        $query->condition('ps.brand_name', '%' . db_like($form_state['values']['brand']) . '%', 'LIKE');
    }
    //$query->addField('town', 'name', 'town');
    $query->limit(variable_get("list_page_item_number", 5));
    if ($form_state['triggering_element']['#value'] != '查询')
        $query->orderBy('ps.timestamp', 'DESC');
    $product_skuInfo = $query->orderByHeader($header)->execute();

    $rows = array();
    //表内容
    $n = 1;
    $user_role_name = get_user_role_name($user->uid);
    foreach ($product_skuInfo as $row) {
        $checked = '<input type="checkbox"  class="listViewTableCheck"  value ="' . $row->product_sku_id . '"/>';
//                $checked = '<input type="checkbox" disabled="true" class="listViewTableCheck"' . ' value ="'.$row->product_sku_id . '"/>';
        $rows[] = array(
            array('data' => $checked, 'class' => ['no']),
            array('data' => $row->brand_name, 'class' => ['province']),
            array('data' => $row->variety_name, 'class' => 'name'),
            array('data' => $row->proportion, 'class' => 'proportion'),
            array('data' => $row->SM, 'class' => 'SM'),
            array('data' => '<a href="' . base_path() . 'product_sku/edit/' . $row->product_sku_id . '" data-toggle="modal" target = "_top"><img width="20px" height="20px" src="' . base_path() . 'sites/all/themes/tweme/images/images/cz.png"/></a>', 'class' => 'more'),
        );
    }
    $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover')))); //表格
    /*	 * *按钮** */
    if (sizeof($rows) > 0) {
        $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
        $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
    }
    $output .= theme('pager');
    return $output;
    // }
}
function product_sku_delete()
{
    ajax_table_delete('product_sku', 'product_sku_id');
}